﻿<%@ WebHandler Language="C#" Class="wxMiniProgramLogin" %>
using Models;
using System;
using System.Web;
using Newtonsoft.Json;
using System.Linq;
using System.Web.Configuration;
using Newtonsoft.Json.Linq;


public class wxMiniProgramLogin : IHttpHandler {
    public static string APPID =WebConfigurationManager.AppSettings["APPID"];
    public static string APPSECRET =WebConfigurationManager.AppSettings["APPSECRET"];
    public void ProcessRequest (HttpContext context) {
        context.Response.ContentType = "text/plain";

        //encryptedData: "tCw="
        //iv: "scOln9/Qqzc/lxoGfmaunw=="
        //session_key: "VACD4DwcRvTYaw5PaBcbQQ=="
        //signature: "27256f918f8ba0ec14631985e4ec9409cc787b05"

       var encryptedData=HttpContext.Current.Request.Form["encryptedata"];
       var iv=HttpContext.Current.Request.Form["iv"];
       var sessionKey=context.Request.Headers["access_token"];//Header中的token
        string openid = context.Request.Headers["X-ACCESS-USER"];//Header中的token

        ///public static string Decrypt(string encryptedData, string iv, string sessionKey)
        var wxresult= wxHelper.Decrypt(encryptedData,iv,sessionKey);
       


         var mytoken = (JObject)JsonConvert.DeserializeObject(wxresult);

        var phoneNumber = mytoken["phoneNumber"].ToString();

        using (Entities db = new Entities())
        {
            var wxuser = db.User.FirstOrDefault(p => p.UOpenID == openid);

            wxuser.UTel =phoneNumber;


            db.SaveChanges();

        };

        var data = new
        {
            data =new
            {

                token=openid
            },
            code = 1,
            msg = "登陆",
            time = DateTime.Now
        };
        Formatting microsoftDataFormatSettings = default(Formatting);
        string result = JsonConvert.SerializeObject(data, microsoftDataFormatSettings);
        context.Response.Write(result);

        HttpContext.Current.Response.End();
        //context.Response.Write("Hello World");


    }





    public bool IsReusable {
        get {
            return false;
        }
    }

}